package com.site.blog.my.core.interceptor;

import com.site.blog.my.core.dao.TbResourceMapper;
import com.site.blog.my.core.dao.TbRoleResourceMapper;
import com.site.blog.my.core.dao.TbUserRoleMapper;
import com.site.blog.my.core.entity.*;
import com.site.blog.my.core.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 后台系统身份验证拦截器
 *
 * @author 13
 * @qq交流群 796794009
 * @email 2449207463@qq.com
 * @link http://13blog.site
 */
@Component
public class AdminLoginInterceptor implements HandlerInterceptor {

    @Autowired
    AdminUserService adminUserService;
    @Resource
    TbUserRoleMapper tbUserRoleMapper;

    @Resource
    TbRoleResourceMapper tbRoleResourceMapper;

    @Resource
    TbResourceMapper tbResourceMapper;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String uri = request.getRequestURI();
        if (uri.startsWith("/admin") && null == request.getSession().getAttribute("loginUser")) {
            request.getSession().setAttribute("errorMsg", "请重新登陆");
            response.sendRedirect(request.getContextPath() + "/admin/login");
            return false;
        } else {
            if(getRole(request,response,o)==false){
                response.sendRedirect(request.getContextPath()+"/Role/noRole");
                return false;
            }
            request.getSession().removeAttribute("errorMsg");
            return true;
        }
    }

    public boolean getRole(HttpServletRequest httpServletRequest, HttpServletResponse response, Object o){
        //1.获取用户
        Integer userId = (Integer)httpServletRequest.getSession().getAttribute("loginUserId");
        //2.联查得到权限 select roleid by userid
        TbUserRoleExample example = new TbUserRoleExample();
        TbUserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        //查到当前用户下所有角色
        List<TbUserRole> role = tbUserRoleMapper.selectByExample(example);
        //通过角色去查找所有的资源权限
        Set <TbRoleResource>set = new HashSet<>();
        role.forEach(ox->{
            TbRoleResourceExample examplex = new TbRoleResourceExample();
            TbRoleResourceExample.Criteria  criteria1 = examplex.createCriteria();
            criteria1.andRoleIdEqualTo(ox.getRoleId());
            List<TbRoleResource> resourceId = tbRoleResourceMapper.selectByExample(examplex);
            set.addAll(resourceId);
        });
        //3.通过url得到当前正在访问的资源
        String path = httpServletRequest.getRequestURI();
        //4.如果相同抛出异常 不能删除
        final boolean[] s = {true};
        set.forEach(ox->{
            if(path.startsWith(tbResourceMapper.selectByPrimaryKey(ox.getResourceId()).getResourceurl()) ){
                s[0] = false;
            }
        });
        return s[0];
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
